﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Entity;
using SqlSugar;
using System.Reflection;

namespace Vue3Net7.Controllers
{    
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ToolController : ControllerBase
    {
        private readonly ISqlSugarClient _db;

        public ToolController(ISqlSugarClient db)
        {
            _db = db;            
        }

        [HttpGet]
        public async Task<bool> CodeFirst()
        {
            //1.创建数据库
            _db.DbMaintenance.CreateDatabase();
            //2.通过反射加载程序集读取到所有的类型然后根据实体创建表
            string nspace = "Model.Entity";
            Type[] ass = Assembly.LoadFrom($"{AppContext.BaseDirectory}Model.dll")
                .GetTypes().Where(p => p.Namespace == nspace).ToArray(); 
            //3、CodeFirst创建数据库表
            _db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);
            //4、初始化超级管理员和菜单
            Users user = new Users()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "admin",
                NickName = "超级管理员",
                Password = "123456",
                UserType = 0,
                isEnable = true,
                Description = "数据库初始化时默认添加的超级管理员",
                CreateDate = DateTime.Now,
                CreateUserId = ""
            };
            string userId = (await _db.Insertable(user).ExecuteReturnEntityAsync()).Id;

            Menu menuManagement = new Menu() 
            {
                Id = Guid.NewGuid().ToString(),
                Name = "菜单管理",
                Index = "/menu",
                FilePath = "menu.vue",
                ParentId = "",
                order = 1,
                IsEnable = true,
                Icon = "folder",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            string menuManagementId = (await _db.Insertable(menuManagement).ExecuteReturnEntityAsync()).Id;

            Menu menuList = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "菜单列表",
                Index = "/menu",
                FilePath = "menu.vue",
                ParentId = menuManagementId,
                order = 1,
                IsEnable = true,
                Icon = "notebook",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            await _db.Insertable(menuList).ExecuteCommandAsync();

            Menu menuRoles = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "角色管理",
                Index = "/role",
                FilePath = "role.vue",
                ParentId = "",
                order = 1,
                IsEnable = true,
                Icon = "folder",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            string menuRolesId = (await _db.Insertable(menuRoles).ExecuteReturnEntityAsync()).Id;

            Menu menuRolesList = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "角色列表",
                Index = "/role",
                FilePath = "role.vue",
                ParentId = menuRolesId,
                order = 1,
                IsEnable = true,
                Icon = "notebook",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            await _db.Insertable(menuRolesList).ExecuteReturnEntityAsync();

            Menu menuUser = new Menu()
            { 
                Id = Guid.NewGuid().ToString(),
                Name = "用户管理",
                Index = "/user",
                FilePath = "user.vue",
                ParentId = "",
                order = 1,
                IsEnable = true,
                Icon = "folder",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            string menuUserId = (await _db.Insertable(menuUser).ExecuteReturnEntityAsync()).Id;

            Menu menuUserList = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "用户列表",
                Index = "/user",
                FilePath = "user.vue",
                ParentId = menuUserId,
                order = 1,
                IsEnable = true,
                Icon = "notebook",
                Decription = "数据库初始化时默认添加的默认菜单",
                CreateTime = DateTime.Now,
                CreateUserId = userId
            };
            return await _db.Insertable(menuUserList).ExecuteCommandIdentityIntoEntityAsync();
        }
    }
}
